package mediumLRU缓存;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class LRUCache extends LinkedHashMap{
    private int capacity = 0;
    private int number = 0;
    Map<Integer,Integer> map = new LinkedHashMap<>(0,1.6f,true);
    public LRUCache(int capacity) {
        this.capacity = capacity;
    }

    public int get(int key) {
        return  map.getOrDefault(key,-1);
    }

    public void put(int key, int value) {
        int i = map.getOrDefault(key,-1);
        if (i!=-1){
            map.put(key,value);
        }
        else {
            if (number < capacity) {
                map.put(key, value);
                number++;
            }
            else{
                map.remove(map.keySet().iterator().next());
                map.put(key,value);
            }
        }
    }
}
class LRUCache1 extends LinkedHashMap<Integer, Integer>{
    private int capacity;

    public LRUCache1(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    public int get(int key) {
        return super.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}

